const express = require("express");
const cors = require("cors");
const path = require("path");
const app = express();
const jwt = require("express-jwt");
app.listen(3000, () => console.log("大事件服务启动"));

// 配置应用级别的中间件
app.use(cors());
app.use(express.urlencoded({ extended: false }));
app.use(jwt({ secret: "bigevent" }).unless({ path: /^\/api/ }));
// 加载路由模块
app.use("/api", require(path.join(__dirname, "routers", "login")));

app.use("/my/artical", require(path.join(__dirname, "routers", "category")));

app.use("/my/artical", require(path.join(__dirname, "routers", "artical")));

app.use("/my", require(path.join(__dirname, "routers", "user")));

app.use(function(err, req, res, next) {
    if (err.name === "UnauthorizedError") {
        console.log(err.message);
        res.status(401).send({ status: 1, message: "身份认证失败！" });
    }
});